1257F - Make Them Similar - CodeForces Solution


bitmasks brute force hashing meet-in-the-middle *2400

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

const int NMAX = 100;
const int BITSMAX = 30;
const int MIDDLE = BITSMAX / 2;

int a[NMAX];
int n;

int cnt[NMAX];

int diff[NMAX - 1];

int ans = -1;

struct Trie {
  struct node {
    int end = -1;
    map<int, int> link;
  } newNode;

  vector<node> trie;

  Trie() {
    trie.push_back(newNode);
  }

  void add(int mask) {
    int node = 0;
    for (int i = 0; i < n - 1; i++) {
      int p = diff[i];
      if (trie[node].link[p] == 0) {
        trie[node].link[p] = trie.size();
        trie.push_back(newNode);
      }
      node = trie[node].link[p];
    }
    // trie[node].end = mask;
    trie[node].end = (trie[node].end == -1 ? mask : trie[node].end);
  }

  int query() {
    int node = 0;
    for (int i = 0; i < n - 1; i++) {
      int p = diff[i];
      if (trie[node].link[p] == 0)
        return -1;
      node = trie[node].link[p];
    }
    return trie[node].end;
  }
};

void init_read() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
}

void read_data() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
}

/// @brief returns how many bits of 1 x has
/// @param x
int popcount(int x) {
  x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
  x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
  x = ((x >> 4) + x) & 0x0f0f0f0f;
  x = ((x >> 8) + x) & 0x00ff00ff;
  x = ((x >> 16) + x) & 0x0000ffff;
  return x;
}

/// @brief builds vector of consecutive popcount differences
/// @param mask - the digits of the half-th half get xor-ed by mask
/// @param half - 0 for the first half, 1 for the second half
void build_diff(int mask, int half) {
  for (int i = 0; i < n; i++)
    cnt[i] = popcount(((a[i] >> (MIDDLE * half)) & ((1 << MIDDLE) - 1)) ^ mask);

  int sign = (half == 0 ? +1 : -1);

  for (int i = 1; i < n; i++)
    diff[i - 1] = sign * (cnt[i] - cnt[i - 1]);
}

void solve() {
  Trie trie;
  for (int mask = 0; mask < (1 << MIDDLE); mask++) {
    build_diff(mask, 0);
    trie.add(mask);
  }
  for (int mask = 0; mask < (1 << MIDDLE); mask++) {
    build_diff(mask, 1);
    int mask2 = trie.query();
    if (mask2 != -1) {
      ans = (mask << MIDDLE) + mask2;
      return;
    }
  }
}

void check(int mask) {
  if (mask == -1) {
    cout << "OK\n";
    return;
  }
  int checker = popcount(a[0] ^ mask), i = 1;
  while (i < n && popcount(a[i] ^ mask) == checker)
    i++;

  cout << (i == n ? "OK\n" : "WRONG\n");
}

void print_data() {
  cout << ans << '\n';
  // check(ans);
}

int main() {
  init_read();
  read_data();
  solve();
  print_data();

  return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST